<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/9/5
 * Time: 11:05
 */

/**
 * Created by wanglu on 2017/9/5.
 */
class Lang_service extends MY_Service
{


    public function __construct()
    {
        parent::__construct();
        $this->load->model('lang_model');
        $this->load->model('lang_user_model');
        $this->load->model('project_content_temp_model');
    }

    public function get_lang_user($lang_id, $uid) {
        return $this->lang_user_model->getRecord(array('lang_id'=> $lang_id, 'uid'=> $uid));
    }


    public function get_langs_by_pid($pid) {
      return $this->lang_model->getRecords(array('pid'=> $pid), array('createtime', "DESC"));
    }

    public function add_lang_return_id($pid, $name, $shorthand, $remark) {


        $langextis = $this->lang_model->getRecord(array('shorthand' => $shorthand, 'pid'=> $pid));

        if (empty($langextis)) {
            $lang = array(
                'pid'=> $pid,
                'name'=> $name,
                'shorthand' => $shorthand,
                'remark' => $remark,
                'createtime' => time()
            );

            return $this->lang_model->createRecordReturnId($lang);
        } else {

            return $langextis['id'];
        }
    }

    /**
     * 添加语言
     * @param $pid
     * @param $name
     * @param $shorthand
     * @param $remark
     * @return mixed
     * @throws AppException
     */
    public function add_lang($pid, $name, $shorthand, $remark) {
        $langextis = $this->lang_model->getRecord(array('shorthand' => $shorthand, 'pid'=> $pid));
        if (!empty($langextis)) {
            throw  new AppException(ErrorCode::FAIL, " lang shorthand is exits add faill!");
        }

        $lang = array(
            'pid'=> $pid,
            'name'=> $name,
            'shorthand' => $shorthand,
            'remark' => $remark,
            'createtime' => time()
        );

        return $this->lang_model->createRecordReturnId($lang);
    }

    /**
     * 修改语言
     * @param $pid
     * @param $lang_id
     * @param $name
     * @param $shorthand
     * @param $remark
     * @throws AppException
     */
    public function update_lang($pid, $lang_id, $name, $shorthand, $remark) {
        $langextis = $this->lang_model->getRecord(array('id' => $lang_id, 'pid'=> $pid));
        if (empty($langextis)) {
            throw  new AppException(ErrorCode::FAIL, " lang shorthand is not exits!");
        }

        //修改了缩写
        if ($langextis['shorthand'] != $shorthand) {

            //查询是否有同名存在
            $short_langextis = $this->lang_model->getRecord(array('shorthand' => $shorthand, 'pid'=> $pid));
            if (!empty($short_langextis)) {
                throw  new AppException(ErrorCode::FAIL, " lang shorthand is exits add fail, please change user shorthand!");
            }
        }
        
        $lang_data = array(
            'name'=> $name,
            'shorthand' => $shorthand,
            'remark' => $remark
        );

        return $this->lang_model->updateRecord($lang_data,  array('id'=> $lang_id));
    }



    public function delete_lang($lang_id)  {

        //还要 删除...
        $this->project_content_temp_model->deleteRecord(array('lang_id'=> $lang_id));
        $this->lang_user_model->deleteRecord(array('lang_id'=> $lang_id));
        return $this->lang_model->deleteRecord(array('id'=> $lang_id));
    }

    public function get_lang_by_id($lang_id) {
        return $this->lang_model->getRecord(array('id'=> $lang_id));
    }

    /**
     * 获取用户权限
     * @param $pid
     * @param $uid
     * @return mixed
     */
    public function get_lang_permission($pid, $uid) {
        return $this->lang_user_model->getRecords($where = array('pid'=> $pid, 'uid'=> $uid ), array('createtime', 'DESC'));
    }
}
